//
//  ScrollPhotoView.m
//  NaRiLSVMT
//
//  Created by zhangmeng on 14-8-11.
//  Copyright (c) 2014年 helloyouth.org. All rights reserved.
//

#import "ScrollPhotoView.h"
#import "Network_Operation.h"
#import "ScrollPhotoView+Request.h"
#import "UIImageView+AFNetworking.h"
#import "ScrollPhotoView+AddopenView.h"
#import "ScreenPreviewView.h"
#import "MBProgressController.h"
#import "ScrollPhotoView+list.h"
#import "ScrollPhotoView+PlayFunction.h"
extern  BOOL    isFlowProjectPlay;//是哪个模块进行的双击大图操作(流程控制or屏幕预览)

#define kItemWidth   240.0
@interface ScrollPhotoView()
{
    //UILabel *labList;
   
}
@end
@implementation ScrollPhotoView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (void)awakeFromNib {
    self.backgroundColor = ColorRGB(17,43,50);
    self.thumbScreen = [NSMutableArray array];
    self.clipsToBounds = YES;
    self.tuocloseView = LoadFromNib(@"HeadItem");
    self.tuoopenView = LoadFromNib(@"HeadOpenView");
    setViewCorner(self.tuocloseView,8);
    setViewCorner(self.tuoopenView,8);
    self.tag = 4562;
    [self getScreenRequestNum];
}

- (void)reloadHeadImageData:(id)data {

    if([self.delegate respondsToSelector:@selector(didCLickScreen:)]){
        [self.delegate didCLickScreen:StringFromId(self.screenArr[0][@"name"])];
        self.screenNum = StringFromId(self.screenArr[0][@"name"]);
    }
    //Ui
    self.ocflag = CloseScreen;
    self.showView = [[UIView alloc] init];
    self.showView.frame = Frame(0,0,Screen_Width-28,354);
    self.showView.layer.borderWidth = 1.5;
    self.showView.layer.borderColor = ColorRGB(133,183,184).CGColor;
    setViewCorner(self.showView,6);
    //  scan_showimage@2x.png
    self.showView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.showView];
    
    self.showImage = [[UIImageView alloc] init];
    self.showImage.frame = self.showView.frame;
    self.showImage.backgroundColor = ClearColor;
 //   [self.showImage setImage:[UIImage imageNamed:@"scan_showimage@2x.png"]];
    
    UIImageView *image = [[UIImageView alloc] init];
    image.frame = Frame(0,269,Screen_Width-28,85);
    image.backgroundColor = ClearColor;
    [image setImage:[UIImage imageNamed:@"scan_showimage.png"]];
    [self.showView addSubview:image];
    [self.showView addSubview:self.showImage];
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.showView addGestureRecognizer:panGestureRecognizer];
    self.switchView  = [[UIView alloc] init];
    self.switchView.frame = Frame(0,360,Screen_Width-28,45);
    self.switchView.clipsToBounds = YES;
    self.switchView.backgroundColor = ColorRGB(24,78,87);
    [self addSubview:self.switchView];

    self.allthumb = [[UILabel alloc] init];
    self.allthumb.frame = Frame(190,0,250,35);
    //self.allthumb.text = @"共画面";
    NSArray *tmparr = self.nowPlaydata[StringFromInt(self.nowScreen)];
    self.allthumb.text = [NSString stringWithFormat:@"%@ %d / %@%lu%@",@"当前投屏:",1,@"共",(unsigned long)[tmparr count],@"画面"];
    self.allthumb.font = Font(19.0);
    self.allthumb.textColor = [UIColor whiteColor];
    self.allthumb.backgroundColor = [UIColor clearColor];
    [self.switchView addSubview:self.allthumb];
    self.allthumb.hidden = NO;

    self.onceplay = [UIButton buttonWithType:UIButtonTypeCustom];
    self.onceplay.frame = Frame(845,3,36,36);
    [self.onceplay setBackgroundImage:[UIImage imageNamed:@"scan_onceplay.png"] forState:UIControlStateNormal];
    [self.onceplay addTarget:self action:@selector(onceplayClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.switchView addSubview:self.onceplay];
    
    self.arrowplay = [UIButton buttonWithType:UIButtonTypeCustom];
    self.arrowplay.frame = Frame(895,3,36,36);
    [self.arrowplay setBackgroundImage:[UIImage imageNamed:@"scan_arrowplay.png"] forState:UIControlStateNormal];
    [self.arrowplay addTarget:self action:@selector(arrowplayClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.switchView addSubview:self.arrowplay];
    
    self.openClose = [UIButton buttonWithType:UIButtonTypeCustom];
    self.openClose.frame = Frame(945,2,49,45);
    self.openClose.backgroundColor = ColorRGB(70,110,117);
   // [self.openClose setBackgroundImage:[UIImage imageNamed:@"scan_openthumb.png"] forState:UIControlStateNormal];
     setViewCorner(self.openClose,5);
    [self.openClose addTarget:self action:@selector(openCloseClick:) forControlEvents:UIControlEventTouchUpInside];
    self.opencimageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"scan_openthumb.png"]];
    self.opencimageView.frame = CGRectMake(8,6,32,32);
    [self.openClose addSubview:self.opencimageView];
    
    [self.switchView addSubview:self.openClose];
    
    self.timeControl = [[UILabel alloc] initWithFrame:Frame(700,4,50,36)];
    self.timeControl.font = Font(19.0);
    self.timeControl.textColor = [UIColor whiteColor];
    self.timeControl.backgroundColor =  ClearColor;
    [self.timeControl setText:@"间隔:"];
    [self.switchView addSubview:self.timeControl];
    
    self.secondNumTextf = [[UITextField alloc] initWithFrame:CGRectMake(750,4,70,36)];
    self.secondNumTextf.borderStyle = UITextBorderStyleRoundedRect;
    self.secondNumTextf.backgroundColor = ColorRGB(60,184,189);
   // self.secondNumTextf.placeholder = @"10 S";
    self.secondNumTextf.text = @"5";
    self.secondNumTextf.textColor = [UIColor whiteColor];
    self.secondNumTextf.font = Font(20);
    self.secondNumTextf.keyboardType = UIKeyboardTypeDecimalPad;
    self.secondNumTextf.delegate = self;
    [self.switchView addSubview:self.secondNumTextf];
    
    self.textFiledView = [[UIView alloc] initWithFrame:CGRectMake(750,4,70,36)];
    self.textFiledView.backgroundColor = [UIColor grayColor];
    self.textFiledView.alpha = 0.8;
    setViewCorner(self.textFiledView,4);
    [self.switchView addSubview:self.textFiledView];
    self.textFiledView.hidden = YES;
 //  self.secondNumTextf.enabled = NO;
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(15,0,50,35)];
    lab.textColor = [UIColor whiteColor];
    lab.font = [UIFont systemFontOfSize:19.0];
    lab.backgroundColor = [UIColor clearColor];
    lab.text = @"主题:";
    [self.switchView addSubview:lab];
    
    self.btnList = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.btnList.frame = CGRectMake(80,2,85,35);
    
    self.btnList.backgroundColor = ColorRGB(82,172,172);
   // [self.btnList setTintColor:[UIColor whiteColor]];
    self.btnList.layer.borderColor = ColorRGB(82,172,172).CGColor;
    self.btnList.layer.borderWidth = 1.0;
    setViewCorner(self.btnList,6.0);
    [self.btnList addTarget:self action:@selector(btnListClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.switchView addSubview:self.btnList];
    
    self.labList = [[UILabel alloc] initWithFrame:CGRectMake(9,0,45,34)];
    self.labList.font = [UIFont systemFontOfSize:18.0];
    self.labList.tag = 0;
    //  lablist.backgroundColor = [UIColor redColor];
    self.labList.textColor = [UIColor whiteColor];
    self.labList.text = @"全部";
    [self.btnList addSubview:self.labList];
    self.imageList = [[UIImageView alloc] initWithFrame:CGRectMake(62,15,13,10)];
    self.imageList.image = [UIImage imageNamed:@"scan_uplist.png"];
    [self.btnList addSubview:self.imageList];
    
    self.collView = [[UIView alloc] initWithFrame:Frame(0,403,Screen_Width-28,175)];
    self.collView.backgroundColor = ColorRGB(25,76,86);
    self.collView.layer.borderWidth = 1.5;
    self.collView.tag = 1520;
    self.collView.clipsToBounds = YES;
    self.collView.layer.borderColor = ColorRGB(133,183,184).CGColor;
    setViewCorner(self.collView,6);
    [self addSubview:self.collView];
    
    self.proBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.proBtn.frame = Frame(0,0,40,175);
    //self.proBtn.backgroundColor = [Common colorFromHexRGB:@"26a7ad"];//4C96A0
    [self.proBtn setBackgroundImage:[Common drawImageSize:CGSizeMake(40, 175) Color: [Common colorFromHexRGB:@"17888d"]] forState:UIControlStateSelected];
    [self.proBtn setBackgroundImage:[Common drawImageSize:CGSizeMake(40, 175) Color: [Common colorFromHexRGB:@"022a2c"]] forState:UIControlStateNormal];
   // self.proBtn.backgroundColor = [UIColor redColor];
    //[self.proBtn setBackgroundImage:[UIImage imageNamed:@"scan_play_left.png"] forState:UIControlStateNormal];
    [self.proBtn addTarget:self action:@selector(propageClick:) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *proimage = [[UIImageView alloc] initWithFrame:CGRectMake(10,70,20,30)];
    proimage.image = [UIImage imageNamed:@"scan_play_left.png"];
    [self.proBtn addSubview:proimage];
    [self.collView addSubview:self.proBtn];
    
    self.nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextBtn.frame = Frame(Screen_Width-62,0,40,175);
  //  self.nextBtn.backgroundColor = [Common colorFromHexRGB:@"26a7ad"];
        [self.nextBtn setBackgroundImage:[Common drawImageSize:CGSizeMake(40, 175) Color: [Common colorFromHexRGB:@"022a2c"]] forState:UIControlStateNormal];
    [self.nextBtn setBackgroundImage:[Common drawImageSize:CGSizeMake(40, 175) Color: [Common colorFromHexRGB:@"17888d"]] forState:UIControlStateSelected];

    //self.nextBtn.alpha = 0.1;
    //[self.nextBtn setBackgroundImage:[UIImage imageNamed:@"scan_play_right.png"] forState:UIControlStateNormal];
    UIImageView *nextimage = [[UIImageView alloc] initWithFrame:CGRectMake(10,70,20,30)];
    nextimage.image = [UIImage imageNamed:@"scan_play_right.png"];
    [self.nextBtn addSubview:nextimage];

    [self.nextBtn addTarget:self action:@selector(nextpageClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.collView addSubview:self.nextBtn];
    //获取当前的screenId
    self.screenId = StringFromId(self.screenArr[0][@"id"]);
    if(self.screenArr){
        [self getScreenGroup:self.screenArr[0][@"id"] Numscreen:0];
    }
    self.direction = EPanDirectionNormal;
    self.clickNum = 0;
    self.pageView = [[UIPageControl alloc] initWithFrame:CGRectMake(450,580,100,30)];
    self.pageView.numberOfPages  = 0;
    self.pageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    self.pageView.hidesForSinglePage = YES;
    self.pageView.currentPage = 0;
    self.pageView.currentPageIndicatorTintColor = [Common colorFromHexRGB:@"ffffff"];
    self.pageView.pageIndicatorTintColor = [UIColor grayColor];
    [self addSubview:self.pageView];
    
    [self initDroplist];
    
    self.listProView = [[UIView alloc] initWithFrame:CGRectMake(0,0,Screen_Width, Screen_Height)];
    self.listProView.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *tapList = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapListView:)];
    [self.listProView addGestureRecognizer:tapList];
    
    //播放器 开关 同步
    [AddNoticeObserver(NoticeKey_TcpDeviceGetScreen) {
        NSString *str = [note object];
        NSDictionary *dic1 = [Common jsontoArray:str];
        NSDictionary *dic2 =  dic1[@"messageData"];
        NSDictionary *dic3 = dic2[@"message"];
        NSArray *tmparr = dic3[@"Params"];
        [self deviceGetscreendata:tmparr[0][@"Value"] Group:tmparr[1][@"Value"]];
    }];
    //切屏 同步
    [AddNoticeObserver(NoticeKey_TcpUpateClient) {
        NSString *str = [note object];
        NSDictionary *dic1 = [Common jsontoArray:str];
        NSDictionary *dic2 =  dic1[@"messageData"];
        NSDictionary *dic3 = dic2[@"message"];
        NSArray *arr = dic3[@"Params"];
        NSString *str1 = arr[0][@"Value"]; // screenid
        NSString *str2 = arr[2][@"Value"];
        [self upateClient:str1 HostId:str2];
    }];
    
    //刷新缩略图
    [AddNoticeObserver(NoticeKey_flushThumb) {
        [self flushThumb:[self.nowPlaydata[@"groupNum"] integerValue]];
    }];
}
-(void)viewDidUnload
{
   // [super viewDidUnload];
    self.Allscreenviewcontroll = nil;
    
    self.Forscreenviewcontroll = nil;
    
     self.controller = nil;
     self.showView = nil;
     self.switchView = nil;
     self.collView = nil;
    
    self.onceplay = nil;
    self.arrowplay = nil;
     self.openClose = nil;
    self.tuocloseView = nil;
     self.tuoopenView = nil;
     self.secondNumTextf = nil;
    
     self.proBtn = nil;
    self.nextBtn = nil;
     self.timeControl = nil;
     self.allthumb = nil; // 共多少个屏幕
     self.showImage = nil;
    self.sourceHeads = nil;
    //Strong UIView *operateView;
    self.screenArr = nil; //屏幕个数
    self.thumbScreen = nil;//所有屏幕的数据
    self.collecView = nil; //当前在的屏幕
    self.nowPlaydata = nil;
    
    self.playtime = nil; // 屏幕播放的秒数
    
    self.screenNum = nil;
    
    self.screenId = nil;
}
- (void)didReceiveMemoryWarning
{
   // [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    self.Allscreenviewcontroll = nil;
    
    self.Forscreenviewcontroll = nil;
    
    self.controller = nil;
    self.showView = nil;
    self.switchView = nil;
    self.collView = nil;
    
    self.onceplay = nil;
    self.arrowplay = nil;
    self.openClose = nil;
    self.tuocloseView = nil;
    self.tuoopenView = nil;
    self.secondNumTextf = nil;
    
    self.proBtn = nil;
    self.nextBtn = nil;
    self.timeControl = nil;
    self.allthumb = nil; // 共多少个屏幕
    self.showImage = nil;
    self.sourceHeads = nil;
    //Strong UIView *operateView;
    self.screenArr = nil; //屏幕个数
    self.thumbScreen = nil;//所有屏幕的数据
    self.collecView = nil; //当前在的屏幕
    self.nowPlaydata = nil;
    
    self.playtime = nil; // 屏幕播放的秒数
    
    self.screenNum = nil;
    
    self.screenId = nil;
}

#pragma -mark
#pragma -mark delegate
- (void)didSelectHeadItem:(HeadItem *)item {
    self.curSelIdx = item.tag;
    
}

-(void)pageClick:(NSInteger)num
{
    //点击屏幕取出－数据 切屏
    int flag = 0;
    int i = 0;
    //是否展开
    self.nowPlaydata[@"noOpen"] = [NSString stringWithFormat:@"%ld",(long)self.ocflag];
    self.nowPlaydata[@"playtime"] = self.secondNumTextf.text;
    //关闭交互
    if([self.delegate respondsToSelector:@selector(panTalkinfo)]){
        [self.delegate panTalkinfo];
    }
    //判断屏幕是否已经加载
    for(NSDictionary *dic in self.thumbScreen){
        NSArray *arr = dic.allKeys;
        for(int j = 0;j<arr.count;j++){
        if([arr[j] isEqualToString:[NSString stringWithFormat:@"%ld",(long)num]]){
            flag = 1;
            break;
        }
        }
        if(flag)
        break;
        i++;
    }
    if(i >= [self.thumbScreen count]){
        i = [self.thumbScreen count] - 1;
    }
    //获取当前的screenId
    self.screenId = StringFromId(self.screenArr[num][@"id"]);
    if(flag == 0){
    self.textFiledView.hidden = YES;
    [self getScreenGroup:self.screenArr[num][@"id"] Numscreen:num];
    }
    else{
        MJCollectionViewController *collection = self.thumbScreen[i][@"collec"];
        [self.collView bringSubviewToFront:collection.view];
        self.collecView = collection;
        self.nowPlaydata = self.thumbScreen[i];
        self.nowScreen = num;
        self.proHostArr = [NSMutableArray arrayWithArray:self.thumbScreen[i][@"proharr"]];
        //判断单次播放的图片
        if([self.thumbScreen[i][@"noOnceplay"] intValue]){
            [self.onceplay setBackgroundImage:[UIImage imageNamed:@"scan_stopplay.png"] forState:UIControlStateNormal];
        }
        else{
            [self.onceplay setBackgroundImage:[UIImage imageNamed:@"scan_onceplay.png"] forState:UIControlStateNormal];
        }
        
        if([self.thumbScreen[i][@"noArrowplay"] intValue]){
         [self.arrowplay setBackgroundImage:[UIImage imageNamed:@"scan_stopplay.png"] forState:UIControlStateNormal];
        }
        else{
        [self.arrowplay setBackgroundImage:[UIImage imageNamed:@"scan_arrowplay.png"] forState:UIControlStateNormal];
        }
        
        if([self.thumbScreen[i][@"noOnceplay"] intValue] || [self.thumbScreen[i][@"noArrowplay"] intValue])
        {
            self.textFiledView.hidden = NO;
        }
        else{
            self.textFiledView.hidden = YES;
        }
        
        if(![self.thumbScreen[i][@"noOpen"] intValue]){
            self.opencimageView.image = [UIImage imageNamed:@"scan_openthumb.png"];
            [self closeThumbUi];
        }
        else{
            self.opencimageView.image = [UIImage imageNamed:@"scan_closethumb.png"];
            [self openThumbUi];
        }
        
        NSMutableArray *grouparr =[NSMutableArray arrayWithArray:self.thumbScreen[i][@"group"]];
        NSInteger grouNum = [self.thumbScreen[i][@"groupNum"] intValue];
         [self didSectionframe:grouparr[grouNum][@"groupname"]];
        [collection.collectionView reloadData];
        self.secondNumTextf.text = StringFromId(self.thumbScreen[i][@"playtime"]);
        self.ocflag = [self.thumbScreen[i][@"noOpen"] intValue];
        
        [self eventNumData:collection.figureNum AllNUm:[self.nowPlaydata[StringFromInt(self.nowScreen)] count]];
        if([self.nowPlaydata[StringFromInt(self.nowScreen)] count] == 0){
            self.showImage.image = nil;
            [[TimerDownImage sharedInstance] downCallBackData:@"" CallBack:^(UIImage *image){
                [self.showImage setImage:image];
            }];
             [[TimerDownImage sharedInstance] stopTimerDownImage];
             self.showImage.image = nil;
        }
        else{
            if(![@"0" isEqualToString:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][collection.clickNum][@"width"])] && ![@"0" isEqualToString:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][collection.clickNum][@"height"])]){
            [self.showImage setImage:[[DownloadImage sharedInstance] getUrlImage:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][collection.clickNum][@"templateId"]) Block:^(UIImage *image){
                [self.showImage setImage:image];
            }]];
            [[TimerDownImage sharedInstance] downCallBackData:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][collection.clickNum][@"templateId"]) CallBack:^(UIImage *image){
                [self.showImage setImage:image];
            }];
            }
            else{
             [[TimerDownImage sharedInstance] stopTimerDownImage];
            self.showImage.image = nil;
               //Alert(@"图片height或width为空");
            }
        }
        //改变pageview
        self.pageView.numberOfPages = collection.allPagenumber;
        self.pageView.currentPage = collection.pageBtnnum - 1;
        
        if(collection.figureNum > -1){
        self.proHostArr = [NSMutableArray arrayWithArray:[self.nowPlaydata[StringFromInt(self.nowScreen)][collection.figureNum][@"hostId"] componentsSeparatedByString:@","]];
        }
        else{
            [self.proHostArr removeAllObjects];
        }
    }
    //数据添加
    if([self.delegate respondsToSelector:@selector(didCLickScreen:)]){
        [self.delegate didCLickScreen:StringFromId(self.screenArr[num][@"name"])];
        self.screenNum = StringFromId(self.screenArr[num][@"name"]);
    }
}
-(void)openCloseClick:(id)sender
{
if([self.nowPlaydata[StringFromInt(self.nowScreen)] count] > CLOSEPAGE){
if([self.nowPlaydata[StringFromInt(self.nowScreen)] count]){
  if(self.ocflag == CloseScreen){
      [self openThumbView];
}
else if(self.ocflag == OpenScreen){
    [self closeThumbView];
    [self.secondNumTextf resignFirstResponder];
}
}
    }
}
-(void)propageClick:(id)sender
{
//[self.proBtn setBackgroundImage:[Common drawImageSize:CGSizeMake(40, 175) Color: [UIColor redColor]] forState:UIControlStateSelected];

    [self proBtnpage];
}
-(void)nextpageClick:(id)sender
{
    [self nextBtnpage];
}
#pragma -mark
#pragma -mark MJCollectionViewControllerDelegate
-(void)didselectCell:(HeadItem *)item Num:(NSInteger)n
{
    self.clickNum = n;
    self.forScreen = NO; // 停止投屏
    self.showImage.image = nil;
    if([self.delegate respondsToSelector:@selector(panTalkinfo)]){
        [self.delegate playTalkinfo];
    }
    if(![@"0" isEqualToString:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][n][@"width"])] && ![@"0" isEqualToString:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][n][@"height"])]){
    [self.showImage setImage:[[DownloadImage sharedInstance] getUrlImage:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][n][@"templateId"]) Block:^(UIImage *image){
        [self.showImage setImage:image];
    }]];
    
    [[TimerDownImage sharedInstance] downCallBackData:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][n][@"templateId"])  CallBack:^(UIImage *image){
        [self.showImage setImage:image];
    }];
    }
    else{
        [[TimerDownImage sharedInstance] stopTimerDownImage];
        self.showImage.image = nil;
        //Alert(@"图片height或width为空");
    }
    //停止播放
    [self stopPlay];
    
}
-(void)stopPlay
{
    [self.arrowplay setBackgroundImage:[UIImage imageNamed:@"scan_arrowplay.png"] forState:UIControlStateNormal];
    self.nowPlaydata[@"noArrowplay"] = @"0";
    self.nowPlaydata[@"startqueue"] = @"0";
    [self.onceplay setBackgroundImage:[UIImage imageNamed:@"scan_onceplay.png"] forState:UIControlStateNormal];
    self.nowPlaydata[@"noOnceplay"] = @"0";
    MJCollectionViewController *collecView = self.nowPlaydata[@"collec"];
    collecView.flagplay = NO;
    self.textFiledView.hidden = YES;
    
}
-(void)panCollectionItem:(NSInteger)num CGRECT:(CGRect)frame
{
    MJCollectionViewController *collecView = self.nowPlaydata[@"collec"];
    if(self.openroclose){
        self.tuoopenView.frame = frame;
        if(self.tuoopenView.frame.origin.y<= 35){
            //投屏幕成功
            [self stopPlay];
            if([self.delegate respondsToSelector:@selector(panTalkinfo)]){
                [self.delegate playTalkinfo];
            }
            self.forScreen = YES;
            [self.tuoopenView removeFromSuperview];
            NSInteger eventNum;
            if(collecView.figureNum > -1){
                eventNum = collecView.figureNum;
            }
            else{
                eventNum = num;
            }
            collecView.clickNum = num;
            collecView.figureNum = num;
            
            [self eventNumData:collecView.figureNum AllNUm:collecView.allPage];
            [collecView.collectionView reloadData];
            if(![@"0" isEqualToString:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][collecView.clickNum][@"width"])] && ![@"0" isEqualToString:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][collecView.clickNum][@"height"])]){
            [self.showImage setImage:[[DownloadImage sharedInstance] getUrlImage:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][collecView.clickNum][@"templateId"]) Block:^(UIImage *image){
                [self.showImage setImage:image];
            }]];
            
            [[TimerDownImage sharedInstance] downCallBackData:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][collecView.clickNum][@"templateId"])  CallBack:^(UIImage *image){
                [self.showImage setImage:image];
            }];
            }
            else{
                [[TimerDownImage sharedInstance] stopTimerDownImage];
                self.showImage.image = nil;
             //  Alert(@"图片height或width为空");
            }
            //发送tcp切屏命令
            
            NSArray *dataarr = self.nowPlaydata[StringFromInt(self.nowScreen)];
            [self eventScreenSendTcpTrue:dataarr[num][@"hostId"]];
            
            NSMutableArray *arr = self.nowPlaydata[@"proharr"];
            if(eventNum == num){
            if(![arr count] || arr == nil){
                }
            else{
               [self eventScreenSendTcpFalse:arr];
            }
            }
            else{

                    [self eventScreenSendTcpFalse:[NSMutableArray arrayWithArray:[dataarr[eventNum][@"hostId"] componentsSeparatedByString:@","]]];
                    arr = [NSMutableArray arrayWithArray:[dataarr[eventNum][@"hostId"] componentsSeparatedByString:@","]];
                    self.nowPlaydata[@"proharr"] = [NSMutableArray arrayWithArray:arr];
                    self.proHostArr = [NSMutableArray arrayWithArray:arr];
            }
        }
        else{
            //__block int n;
            [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
                if((num-((MJCollectionViewController *)self.nowPlaydata[@"collec"]).pagenum) > 3){
                  //  num   = num - 4;
                    self.tuoopenView.frame = Frame((num-4-((MJCollectionViewController *)self.nowPlaydata[@"collec"]).pagenum)*233+36,395,225,150);
                }
                else{
                    self.tuoopenView.frame = Frame((num-((MJCollectionViewController *)self.nowPlaydata[@"collec"]).pagenum)*233+36,235,225,150);
                }
                
            } completion:^(BOOL finish){
                [self.tuoopenView removeFromSuperview];
            }];
        }
        
    }
    else{
        self.tuocloseView.frame = frame;
        if(self.tuocloseView.frame.origin.y <= 220){
        //投屏幕成功
            if([self.delegate respondsToSelector:@selector(panTalkinfo)]){
                [self.delegate playTalkinfo];
            }
            [self stopPlay];
            self.forScreen = YES;
            NSInteger eventNum;
            if(collecView.figureNum > -1){
            eventNum = collecView.figureNum;
            }
            else{
            eventNum = num;
            }
            [self.tuocloseView removeFromSuperview];
            collecView.clickNum = num;
            collecView.figureNum = num;
            [self eventNumData:collecView.figureNum AllNUm:collecView.allPage];
            [collecView.collectionView reloadData];
            if(![@"0" isEqualToString:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][collecView.clickNum][@"width"])] && ![@"0" isEqualToString:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][collecView.clickNum][@"height"])]){
            [self.showImage setImage:[[DownloadImage sharedInstance] getUrlImage:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][collecView.clickNum][@"templateId"]) Block:^(UIImage *image){
                [self.showImage setImage:image];
            }]];
            
            [[TimerDownImage sharedInstance] downCallBackData:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][collecView.clickNum][@"templateId"])  CallBack:^(UIImage *image){
                [self.showImage setImage:image];
            }];
            }
            else{
                [[TimerDownImage sharedInstance] stopTimerDownImage];
                self.showImage.image = nil;
               //Alert(@"图片height或width为空");
            }
            //发送tcp切屏命令
            
            NSArray *dataarr = self.nowPlaydata[StringFromInt(self.nowScreen)];
            [self eventScreenSendTcpTrue:dataarr[num][@"hostId"]];
            
            NSMutableArray *arr = self.nowPlaydata[@"proharr"];
            if(eventNum == num){
                if(![arr count] || arr == nil){
                    
                }
                else{
                    [self eventScreenSendTcpFalse:arr];
                }
            }
            else{
                [self eventScreenSendTcpFalse:[NSMutableArray arrayWithArray:[dataarr[eventNum][@"hostId"] componentsSeparatedByString:@","]]];
                arr = [NSMutableArray arrayWithArray:[dataarr[eventNum][@"hostId"] componentsSeparatedByString:@","]];
                self.nowPlaydata[@"proharr"] = [NSMutableArray arrayWithArray:arr];
                self.proHostArr = [NSMutableArray arrayWithArray:arr];
            }
        }
        else{
        [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
            self.tuocloseView.frame = Frame((num-((MJCollectionViewController *)self.nowPlaydata[@"collec"]).pagenum)*234+40,405,225,150);
        
        } completion:^(BOOL finish){
            [self.tuocloseView removeFromSuperview];
        }];
        }
    
    }
}
-(void)collectionViewforscreenFlag
{
    self.forScreen = YES;
}
#pragma -mark
#pragma -mark TextfileldDelegate
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.secondNumTextf resignFirstResponder];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
{

    if([self.nowPlaydata[@"noOnceplay"] intValue] || [self.nowPlaydata[@"noArrowplay"] intValue] || ![self.nowPlaydata[StringFromInt(self.nowScreen)] count]){
        return NO;
    }
    if(self.ocflag == CloseScreen){
    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
    
        CGRect tr = self.switchView.frame;
        tr.origin.y -= 123;
        self.switchView.frame = tr;
    } completion:^(BOOL finish){
    }];
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.secondNumTextf resignFirstResponder];
    if(self.ocflag == CloseScreen){
    [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        CGRect tr = self.switchView.frame;
        if(tr.origin.y < 360)
        tr.origin.y += 123;
        self.switchView.frame = tr;
        
    } completion:^(BOOL finish){
    }];
    }
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if(self.ocflag == CloseScreen){
    [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        CGRect tr = self.switchView.frame;
        if(tr.origin.y < 370)
        tr.origin.y += 123;
        self.switchView.frame = tr;
        
    } completion:^(BOOL finish){
    }];
    }
}
-(void)pan:(UIPanGestureRecognizer *)recognizer
{
      //关闭下拉框
    if(((ScreenPreviewView *)self.superview).dropListView.isOpen){
        [((ScreenPreviewView *)self.superview) closeListView];
    }
    if([self.delegate respondsToSelector:@selector(panTalkinfo)]){
        [self.delegate panTalkinfo];
    }
    if([self.nowPlaydata[@"noOnceplay"] intValue] || [self.nowPlaydata[@"noArrowplay"] intValue]){
        return ;
    }
    self.forScreen = NO;
    if([self.nowPlaydata[StringFromInt(self.nowScreen)] count]){
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        self.initTouchPosX = [recognizer locationInView:self.showView].x;
    }
    else if (recognizer.state == UIGestureRecognizerStateChanged) { //status change
        CGFloat panAmount = self.initTouchPosX - [recognizer locationInView:self.showView].x;
       // if (self.isExpand) {
            if (panAmount < 0) {
                //右移
                self.direction = EPanDirectionRight;
            }
            else {
            if (panAmount > 0) {
                //左移
                self.direction = EPanDirectionLeft;
            }
        }
    }
    else if (recognizer.state == UIGestureRecognizerStateEnded ||
             recognizer.state == UIGestureRecognizerStateCancelled){
        [self slideOutContentViewInDirection:self.direction];
    }
    }
}
#pragma -mark
#pragma -mark Custom Methods
-(void)fullScreenBtn:(id)sender
{
    if([self.nowPlaydata[@"noOnceplay"] intValue] || [self.nowPlaydata[@"noArrowplay"] intValue]){
        return;
    }
    if([self.screenArr count] && [self.nowPlaydata[StringFromInt(self.nowScreen)] count] && [self.thumbScreen count]){
    MJCollectionViewController *collecView = self.nowPlaydata[@"collec"];
    if([@"0" isEqualToString:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][collecView.clickNum][@"width"])] && [@"0" isEqualToString:StringFromId(self.nowPlaydata[StringFromInt(self.nowScreen)][collecView.clickNum][@"height"])]){
         [[MBProgressController sharedInstance] showTipsOnlyText:@"加载画面缩略图失败!" AndDelay:1.5];
        return;
     }
        //关闭交互
        if([self.delegate respondsToSelector:@selector(panTalkinfo)]){
            [self.delegate panTalkinfo];
        }
        isFlowProjectPlay = NO;
        if(self.forScreen){
            self.Forscreenviewcontroll = [[NRLSVMForScreenViewController alloc] init];
            self.Forscreenviewcontroll.screenNum = self.screenNum;
           NSString *strhostId = self.nowPlaydata[StringFromInt(self.nowScreen)][collecView.clickNum][@"hostId"];
           self.Forscreenviewcontroll.hostId = strhostId;
            
            self.Forscreenviewcontroll.screenData = [NSMutableDictionary dictionaryWithDictionary:self.nowPlaydata[StringFromInt(self.nowScreen)][collecView.clickNum]];
            
            self.Forscreenviewcontroll.forscreenId = self.screenId;
            if([self.delegate respondsToSelector:@selector(clickpresentViewController:Controller:)])
            {
                [self.delegate clickpresentViewController:self Controller:self.Forscreenviewcontroll];
            }
        }
        else{
            self.Allscreenviewcontroll = [[NRLSVMAllScreenViewController alloc] init];
            
            self.Allscreenviewcontroll.clickNum = collecView.clickNum;
            self.Allscreenviewcontroll.arrData = [NSArray arrayWithArray:(NSArray*)(self.nowPlaydata[StringFromInt(self.nowScreen)])];
            self.Allscreenviewcontroll.screenNum = self.screenNum;
            self.Allscreenviewcontroll.forscreenId = self.screenId;
            
            if([self.delegate respondsToSelector:@selector(clickpresentViewController:Controller:)])
            {
                [self.delegate clickpresentViewController:self Controller:self.Allscreenviewcontroll];
            }
        }
    }
}
-(void)closeList
{
    [self closeListView];
}
@end
